onload = function (){
    panduanclick();
    orderDetail();
}

function panduanclick(){
    let order_status = document.querySelectorAll(".order_status>span");
    order_status.forEach(x=>{
        x.addEventListener("click",function (){
            order_status.forEach(y=>{
                y.style.backgroundColor="rgb(112, 57, 57)"
            })
            event.target.style.backgroundColor="black"
        })
    })
    let order_pay= document.querySelectorAll(".order_pay>span");
    order_pay.forEach(x=>{
        x.addEventListener("click",function (){
            order_pay.forEach(y=>{
                y.style.backgroundColor="rgb(112, 57, 57)"
            })
            event.target.style.backgroundColor="black"
        })
    })
}

function chuancanSearch(pageno){
    var array = new Array();
    let order_status = document.querySelectorAll(".order_status>span");
    order_status.forEach(x=>{
        if (x.style.backgroundColor=="black"){
            if (x.innerText=="全部"){
                array.push("");
            }else {
                array.push(x.innerText)
            }
        }
    })
    let order_pay= document.querySelectorAll(".order_pay>span");
    order_pay.forEach(y=>{
        if (y.style.backgroundColor=="black"){
            if (y.innerText=="全部"){
                array.push("");
            }else {
                array.push(y.innerText)
            }
        }
    })
    var begindate = document.querySelector("#begindate").value;
    var enddate = document.querySelector("#enddate").value;
    if (begindate<enddate){
        array.push(begindate)
        array.push(enddate)
    }else if (begindate!="" && enddate==""){
        array.push(begindate)
        array.push("")
    }else if (begindate=="" && enddate!=""){
        array.push("")
        array.push(enddate)
    }else if (begindate=="" && enddate==""){
        array.push("")
        array.push("")
    }else {
        alert("您输入的日期有误！")
        return
    }

    var searchvalue = document.querySelector("#searchvalue").value;
    array.push(searchvalue)

    console.log(array)
    if (isNaN(pageno)){
        pageno=1;
    }
    $.ajax({
        url:"/htorders?v=orderfenye",
        type:"post",
        dataType:"json",
        data:{
            pageno,
            "arr":JSON.stringify(array)
        },
        success:((resp)=>{
            console.log(resp)
            var tbody = document.querySelector("tbody");
            var fenye = document.querySelector(".fenye");
            let tbodyhtml=``;
            let fenyehtml=``;
            resp.pager.data.forEach(p=>{
                tbodyhtml+=`
                    <tr><td><input type="checkbox" value=i++ name=""></td><td>`+p.did+`</td>
                            <td>` +p.dxnumber+ `</td>
                            <td style="cursor: pointer">`
                                    +p.orderDetails.detail.commodity.sname+
                                    +p.orderDetails.detail.commoditycolor.yname+
                                `|`+p.orderDetails.detail.commoditymemory.nname+
                            `</td>
                            <td>`
                                    +p.orderDetails.gnumber+
                            `</td>
                            <td >`
                                    +p.dtotal+
                            `</td>
                            <td >`
                                    +p.pay+
                            `</td>
                            <td class="td-status">`
                                    +p.dstatus+
                            `</td>
                            <td>`
                                    +p.user.uname+
                            `</td>
                            <td >`
                                    +p.dtime+
                            `</td>
                            <td class="td-manage">
                                    <span onclick="shield()" id="${p.did}">详情</span>
                            </td>
                        </tr>`
            })
            tbody.innerHTML=tbodyhtml;
            resp.pager.orderPager.forEach(f=>{
                fenyehtml+=`<span `+(f==resp.pager.pageNo?"class='active'":"")+` onclick="chuancanSearch(`+f+`)">`+f+`</span>`
            })
            fenye.innerHTML=`<span onclick="chuancanSearch(`+(resp.pager.pageNo-1)+`)">`+`上一页</span>`+fenyehtml+
                `<span `+(resp.pager.pageNo==resp.pager.pageCount?"":"onclick='chuancanSearch("+(resp.pager.pageNo+1)+")'")+`>下一页</span>`

            orderDetail();
        })
    })
}

function orderDetail(){
    let detail = document.querySelectorAll(".td-manage>span");
    detail.forEach(x=>{
        x.addEventListener("click",function (){
            let did = x.id;
            $.ajax({
                url:"/htorders?v=orderDetail",
                type: "post",
                data: {did},
                success:(resp=>{
                    //判断订单状态
                    let send_btn = document.querySelector(".send_btn");
                    resp.forEach(r=>{
                        if (r.dstatus!="待发货"){
                            send_btn.style.display="none";
                        }
                    })

                    //数据回显
                    let htnumber = document.querySelector(".htnumber");
                    console.log(htnumber)
                    let htcommodity = document.querySelector(".htcommodity");
                    let htaddress = document.querySelector(".htaddress");
                    let numberhtml = "";
                    let commodityhtml ="";
                    let addresshtml = "";
                    resp.forEach(x=>{
                        numberhtml=` <div style="color: black">订单编号:${x.dxnumber}</div> `
                        htnumber.innerHTML=numberhtml
                        commodityhtml+=`<div style="color: black">
                                            <span>
                                                ${x.orderDetails.detail.commodity.sname}|
                                                ${x.orderDetails.detail.commoditymemory.nname}|
                                                ${x.orderDetails.detail.commoditycolor.yname}
                                            </span>
                                            <img src="${x.orderDetails.detail.commodity.url}" style="width: 60px;height: 60px">
                                        </div>`
                        htcommodity.innerHTML=commodityhtml;
                        addresshtml=`
                                     <div style="margin-left: 17px;margin-bottom: 10px">收货人 :
                                            <span>${x.address.aname}</span>
                                     </div>
                                     <div style="margin-bottom: 10px">联系电话 :
                                            <span>${x.address.aphone}</span>
                                     </div>
                                     <div style="margin-bottom: 10px">收货地址 : 
                                            <span>${x.address.sheng}${x.address.shi}${x.address.qu}${x.address.addressname}</span>
                                     </div>
                                     <div style="margin-bottom: 10px">订单状态 :
                                             <span>${x.dstatus}</span>
                                     </div>
                                     <div style="margin-bottom: 10px">下单时间 :
                                            <span>${x.dtime}</span>
                                     </div>
                                     <div style="margin-bottom: 10px">物流编号 :<input class="logistics" style="margin-left: 10px;color: black" type="text" name="" placeholder="请输入物流编号"></div>
                                    `
                        htaddress.innerHTML=addresshtml;
                    })

                })
            })
        })
    })
}
function updateStatus(){
    let logistics =document.querySelector(".logistics").value;
    console.log(logistics)
    if (logistics==""){
        alert("请先输入物流编号！")
    }else{
        $.ajax({
            url:"/htorders?v=updateStatus",
            type:"post",
            data:{},
            success:function(resp){
                console.log(resp)
                if (resp=="1"){
                    location.href="/htorders";
                }
            }
        })
    }

}

//遮罩层
function shield(){
    let shade = document.querySelector(".shade");
    shade.style.display = "block";

    let popup = document.querySelector(".popup");
    popup.style.display = "block";
}

function cancel_shield(){
    let shade = document.querySelector(".shade");
    shade.style.display = "none";

    let popup = document.querySelector(".popup");
    popup.style.display = "none";
}

